package com.example.zg.zg_312_ordering_new.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.example.zg.zg_312_ordering_new.R;
import com.example.zg.zg_312_ordering_new.activity.FoodListActivity;
import com.example.zg.zg_312_ordering_new.beans.Shop;
import com.example.zg.zg_312_ordering_new.common.Constants;
import com.squareup.picasso.Picasso;


import java.util.List;

public class ShopAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private OnRecyclerViewItemClickListener mOnItemClickListener=null;
    private Context mContext;
    private List<Shop> mDataList;
    private LayoutInflater mLayoutInflater;
    public ShopAdapter(Context mContext,List mDataList){
        this.mContext=mContext;
        this.mDataList=mDataList;
        mLayoutInflater=LayoutInflater.from(mContext);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view =mLayoutInflater.from(viewGroup.getContext()).inflate(R.layout.shop_item,viewGroup,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final Shop entity=mDataList.get(position);
        if (null==entity)
            return;
        ViewHolder viewHolder=(ViewHolder)holder;
        viewHolder.res_name.setText(entity.getShopname());
        viewHolder.res_address.setText(entity.getAddress());
        viewHolder.res_bar.setRating(entity.getLevel());
        Picasso.get().load(Constants.BASE_URL+entity.getPic()).into(viewHolder.image);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext,FoodListActivity.class);
                int id=entity.getShop_id();
                String shopname=entity.getShopname();
                intent.putExtra("id",id);
                intent.putExtra("shopname",shopname);
                intent.putExtra("phonenum",entity.getPhonenum());
                intent.putExtra("shop_id",entity.getShop_id());
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener=listener;
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view,Object data);
    }

    private class ViewHolder extends RecyclerView.ViewHolder{
        RatingBar res_bar;
        TextView res_name,res_address;
        ImageView image;


        public ViewHolder(View view) {
            super(view);
            res_name=(TextView)view.findViewById(R.id.res_name);
            res_bar=(RatingBar)view.findViewById(R.id.ratingBar1);
            image=(ImageView)view.findViewById(R.id.res_image);
            res_address=(TextView)view.findViewById(R.id.res_address);
        }
    }
}

